layui.use(['form','tree','laydate','element','table','laytpl','layer','jquery'],function(){
    var form = layui.form,
        element = layui.element;
    $ = layui.$;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var table = layui.table;
    var laytpl = layui.laytpl;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    //场景数据
    var treeData = {
        elem: '#tree' //指定元素
        , target: '_blank' //是否新选项卡打开（比如节点返回href才有效）
        , click: function (item) { //点击节点回调
            //查询当前场景下的设备
            $.get("/equipment/query?sceneId=" + item.id,function (data) {
                //刷新右侧设备列表和检测项目列表
                $.get("/equipment/query?sceneId=" + item.id,function (data) {
                    $("#equipment-list").html('');
                    $("#equipment-list").append('<option value="">请选择设备</option>');
                    $.each(data.data,function (i,ele) {
                        $("#equipment-list").append('<option value="'+ ele.id +'">'+ ele.name +'</option>');
                    });
                    form.render('select');
                });
            });
        },
        nodes:null
    };
    //异步加载设备所处场景数据
    $.get('/scene/tree',function(data) {
        if (data.data.length != 0) {
            data.data[0].spread = true;
            var children = data.data[0].children;
            if (children != null && children.length != 0) {
                $.each(children,function (i, ele) {
                    ele.spread = true;
                });
            }
        }
        treeData.nodes = data.data;
        layui.tree(treeData);
        $("#tree-scenery").hide();
    });

    //查询条件
    //采集设备数据
    $.get("/equipment/list",function (data) {
        $.each(data.data,function (i,ele) {
            $("#equipment-list").append('<option value="'+ ele.id +'">'+ ele.name +'</option>');
        });
        form.render('select');
    });

    laydate.render({
        elem: '#downAcquisitionTime'
    });
    laydate.render({
        elem: '#upAcquisitionTime'
    });

    //列表数据
    var option = {
        elem: '#tableList',
        url : '/search/datarecord/page',
        cellMinWidth : 95,
        toolbar: '#toolbarDemo',
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 15,
        id : "listTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'equipmentName', title: '所属设备', minWidth:100, align:"center"}
            /*{field: 'channelNo', title: '通道号', minWidth:200, align:'center'},
            {field: 'checkItemName', title: '采集项', minWidth:100, align:"center"},
            {field: 'processedData', title: '数据',  align:'center'},
            {field: 'unit', title: '单位',  align:'center'},
            {field: 'acquisitionTime', title: '采集时间', align:'center',minWidth:150,sort:true, templet:function(d){
                    //处理时间
                    var date = new Date(d.acquisitionTime);
                    /!*console.log(d.acquisitionTime);*!/
                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()+ " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
                }
            }*/
        ]],
        done: function (res, curr, count) {
            layer.close(myindex);
        }
    };

    //搜索设备信息
    $(".search_btn").on("click",function(){
        var equipmentId = $("#equipment-list").val();
        var channelNum = 0;//通道数
        //查询当前设备通道
        $.ajax({
            url : "/equipment/channel/query?equipmentId=" + equipmentId,
            async:false,
            success: function (channel) {
                channelNum = channel.count;
                var checkItemLength = channel.count;
                if (checkItemLength == 0) {
                    return null;
                }
                var cols = [[
                    {type: "checkbox", fixed:"left", width:50},
                    {field: 'equipmentName', title: '所属设备', minWidth:150, align:"center"}
                    ]];
                $.each(channel.data,function (i, ele) {
                    cols[0].push({field: "c_" + ele.collectChannelApplicateTypeId, title: ele.channelName + '('+ ele.originalDataUnit +')', minWidth:100, align:"center"
                    });
                });
                cols[0].push({field: 'acquisitionTime', title: '采集时间', align:'center',minWidth:160,sort:true, templet:function(d){
                        //处理时间
                        var date = new Date(d.acquisitionTime);
                        return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()+ " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
                    }
                });
                cols[0].push({field: 'reportingTime', title: '上报时间', align:'center',minWidth:160,sort:true, templet:function(d){
                    //处理时间
                    var date = new Date(d.reportingTime);
                    /*console.log(d.acquisitionTime);*/
                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()+ " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
                }});
                option.cols = cols;
            }
        });
        if (channelNum == 0) {
            layer.msg("请先配置设备的采集通道！");
            return;
        }
        var upAcquisitionTime = $("#upAcquisitionTime").val();
        var downAcquisitionTime = $("#downAcquisitionTime").val();
        var searchObj = {};
        if(equipmentId == ''){
            layer.msg("请选择搜索设备");
            return;
        }
        /*searchObj.page = 0;
        searchObj.limit = 10;*/
        searchObj.equipmentId = equipmentId;
        searchObj.channelNum = channelNum;
        if (upAcquisitionTime != null && upAcquisitionTime != "") {
            searchObj.upAcquisitionTime = upAcquisitionTime + "T00:00:00Z";
        }
        if (downAcquisitionTime != null && downAcquisitionTime != "") {
            searchObj.downAcquisitionTime = downAcquisitionTime + "T00:00:00Z";
        }
        option.url = '/search/datarecord/page/query/summary';
        option.method = 'post';
        option.where = searchObj;
        myindex = layer.load(2);
        table.render(option);
        /*$.get("/search/datarecord/page/query/summary",searchObj,function (data) {
            console.log(data);
        });*/
    });

});